function sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
        continue;
    }
}

var _$ = $, doing = false, idx = 0, courseCnt = $('.level-2 .level-root a').length;
var globalTask = setInterval(() => {
    console.info('doing: ' + doing + ' idx: ' + idx);
    if (doing) {
        return
    }

    sleep(1000);

    if (idx >= courseCnt) {
        clearInterval(globalTask);
    }

    // 进入课程页
    _$('.level-2 .level-root a')[idx++].click();
    sleep(3000);
    // 开始观看
    console.info(3333);
    _$('.vjs-big-play-button').click();

    // 等待课程播放完
    let lessonTask = setInterval(() => {
        let currentTime = _$('.vjs-current-time-display')[0].innerText;
        let durationTime = _$('.vjs-duration-display')[0].innerText;
        console.info('等待课程播放完: ' + lessonTask + ' 第' + idx + '讲 ' + currentTime + ' => ' + durationTime);
        if (currentTime === durationTime) {
            clearInterval(lessonTask);
            doing = false;
            history.back();
            // sleep(5000);
        }
    }, 2000);
}, 5000);